﻿package {
	import fl.events.SliderEvent;
	import fl.events.ColorPickerEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;

	//For saving bitmap / PNG
	import fl.controls.Button;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import flash.display.BitmapData;

	import idv.etrex.display.*;
	import idv.weki.encoder.*;

	/**
	 * ...
	 * @Author Etrex, extended by Lackneets
	 */
	public class Kaleidoscope extends Sprite {
		var canvas:Xhape = new Xhape();
		var selectedColor:uint=0;
		var selectedStrokeSize:int=3;

		//GUI definition in FLA
		public var container:Sprite;

		public var saveButton:Button;
		public var clearButton:Button;

		public function Kaleidoscope():void {
			super();

			//UI init
			//saveButton.enabled = false;
			saveButton.addEventListener(MouseEvent.CLICK, this.saveHandler);
			// new a canvas
			canvas.x=drawArea.x;
			canvas.y=drawArea.y;
			canvas.scaleX=drawArea.scaleX;
			canvas.scaleY=drawArea.scaleY;
			this.addChild(canvas);

			// add multi graphics
			for (var i:int = 0; i < container.numChildren; i++) {
				if(! (container.getChildAt(i) is Sprite)) continue;
				
				canvas.graphicsBat.add(new FlashGraphics(MovieClip(container.getChildAt(i)).c.graphics));
			}

			//set initial line style
			canvas.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);

			// add mouse paint event
			drawArea.addEventListener(MouseEvent.MOUSE_DOWN, whenMouseDown);
			drawArea.addEventListener(MouseEvent.MOUSE_UP, whenMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_UP, whenMouseUp);

			// add clearButton click event
			clearButton.addEventListener(MouseEvent.CLICK, clearButtonClick);

			// add slider change event
			slider.addEventListener(SliderEvent.CHANGE, sliderChanged);

			// add colorPicker change event
			colorPicker.addEventListener(Event.CHANGE,colorChange);
		}

		// mouse paint event
		public function whenMouseDown(e:MouseEvent):void {

			canvas.graphicsBat.moveTo(e.localX, e.localY);
			drawArea.addEventListener(MouseEvent.MOUSE_MOVE, whenMouseMove);
		}
		public function whenMouseUp(e:MouseEvent):void {
			drawArea.removeEventListener(MouseEvent.MOUSE_MOVE, whenMouseMove);
		}
		public function whenMouseMove(e:MouseEvent):void {
			trace(e.localX, e.localY);
			canvas.graphicsBat.lineTo(e.localX, e.localY);
		}

		public function clearButtonClick(e:MouseEvent):void {
			//clear the canvas
			canvas.graphicsBat.clear();
			canvas.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);
		}

		//select stroke size
		public function sliderChanged(e:SliderEvent):void {
			selectedStrokeSize=e.target.value+1;
			sliderLabel.text="strokeSize: "+selectedStrokeSize;
			canvas.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);
		}

		public function colorChange(e:Event):void {
			selectedColor=ColorPicker(e.target).selectedColor;
			canvas.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);
		}
		private function saveHandler(e:Event) {
			var file:FileReference = new FileReference();
			var bmpData:BitmapData = new BitmapData(850,850);
			bmpData.draw(this.container);
			var pngData:ByteArray=PNGEncoder.encode(bmpData);
			file.save(pngData, "New kaleidoscope.png");
		}
	}
}